package com.example.konwRice;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.konwRice.index.consultFragment;
import com.example.konwRice.index.dataFragment;
import com.example.konwRice.index.knowFragment;
import com.example.konwRice.index.newsFragment;
import com.example.konwRice.index.operateFragment;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link homePage#newInstance} factory method to
 * create an instance of this fragment.
 */
public class homePage extends Fragment  {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public homePage() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment homePage.
     */
    // TODO: Rename and change types and number of parameters
    public static homePage newInstance(String param1, String param2) {
        homePage fragment = new homePage();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home_page, container, false);
        ViewPager2 viewPager2 = (ViewPager2) view.findViewById(R.id.viewPager);//滑动
        TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);//tab


        viewPager2.setAdapter(new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {//frag内容和序号对应
                switch (position) {
                    case 0:
                        return new newsFragment();
                    case 1:
                        return new knowFragment();
                    case 2:
                        return new consultFragment();
                    case 3:
                        return new operateFragment();
                    default:
                        // case 4:
                        return new dataFragment();
                }
            }
            @Override
            public int getItemCount() {//几个页面
                return 5;
            }
        });
        

        new TabLayoutMediator(tabLayout, viewPager2, (tab, position) -> {
            switch (position) {
                case 0:
                    tab.setText("头条");
                    break;
                case 1:
                    tab.setText("知道");
                    break;
                case 2:
                    tab.setText("咨询");
                    break;
                case 3:
                    tab.setText("经营");
                    break;
                case 4:
                    tab.setText("数据");
                    break;
            }
        }).attach();


        return view;

    }

}
